package com.andyliu.sys.controller;

import com.andyliu.sys.pojo.SUser;
import com.andyliu.sys.service.UserService;
import com.andyliu.vo.ParamVO;
import com.andyliu.vo.Result;
import com.andyliu.vo.UserVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserController
 * Description: 用户相关请求
 * date: 2021/1/20 2:55 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理请求", protocols = "application/json")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("save")
    @ApiOperation(value = "保存用户",
            notes = "新增或更新用户信息，id为0或null为新增，否则为更新",
            httpMethod = "POST")
    public Result saveUser(@ApiParam(name = "请求用户的相关信息",
            required = true,
            value = "属性序列化后的值") @RequestBody SUser user) {
        Boolean status = userService.saveUser(user);
        String info="操作成功";
        if(!status){
            info = "操作失败";
        }
        Result result = Result.ok(info,status);
        return result;
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询用户信息",
            notes = "根据id查询用户信息",
            httpMethod = "GET")
    public Result<Object> selectUser(@ApiParam(name = "id", value = "120", required = true) @PathVariable Integer id) {
        SUser user = userService.queryUserById(id);
        Result<Object> rs = Result.ok("",user);
        return rs;
    }

    @PostMapping("query")
    @ApiOperation(notes = "用户列表查询",
            value = "用户列表查询",
            httpMethod = "POST")
    public Result<Object> selectUsers(
            @ApiParam(name = "查询参数")
            @RequestBody ParamVO<SUser> user) {
        PageInfo<UserVO> page = userService.queryUserByPage(user, user.getPageNumber(), user.getPageSize());
        Result<Object> rs = Result.ok("",page);
        return rs;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(notes = "根据id删除用户信息",
            value = "根据id删除用户信息",
            httpMethod = "DELETE")
    public Result<Object> deleteUser(
            @ApiParam(name = "id", value = "需要删除的id", defaultValue = "688", required = true)
            @RequestParam(required = true) Integer id) {
        Boolean status = userService.deleteUserById(id);
        Result<Object> rs = Result.ok(status);
        return rs;
    }

    @DeleteMapping("/deletes")
    @ApiOperation(notes = "批量删除",
            value = "根据id批量删除",
            httpMethod = "DELETE")
    public Result<Object> deleteUsers(
            @ApiParam(name = "ids", value = "需要批量删除的id数组",required = true) Integer[] ids) {
        Boolean status = userService.deleteUsers(ids);
        Result<Object> rs = Result.ok(status);
        return rs;
    }

    @PostMapping("/setUserEnabled")
    @ApiOperation(value = "设置用户启用和停用",
            notes = "设置用户启用停用",
            httpMethod = "POST")
    public Result<Object> setUserEnabled(
            @ApiParam(name = "id", value = "需要启用｜停用的id")
            @RequestParam(required = true) Integer id,
            @ApiParam(name = "status", value = "需要启用或停用")
            @RequestParam(required = true) Integer status) {
        SUser user = new SUser();
        user.setId(id);
        user.setStatus(status);
        Boolean flag = userService.saveUser(user);
        Result<Object> rs = Result.ok(flag);
        return rs;
    }

}
